# REPLICATION CODE
require(mosaic)   # Load additional packages here 
library(readxl)
library(stringr)
library(Zelig)
library(sem)

# begin by uploading the data from the excel file.
d <- read_excel("Q1105 Deepwater Horizon Client Weighted 082317.xlsx", sheet = 1)

### COMMUNITY IDENTIFIERS
# identify the communities for each of the respondents.

terrebonne <- c(70344, 70353, 70356, 70360, 70363, 70397, 70395, 70377, 70359)
lafourche <- c(70345, 70354, 70355, 70373, 70374, 70375, 70394, 70357, 70301, 70343, 70364,
               70030)
grand.isle <- c(70358)
plaquemines <- c(70121, 70037, 70041, 70081, 70083, 70091, 70040)

id.community = function(d) {
  if (d %in% terrebonne) {
    return(1)
  } else if (d %in% lafourche) {
    return(2)
  } else if (d %in% grand.isle) {
    return(3) 
  } else if (d %in% plaquemines) {
    return(4)
  } else {
    return(NA)
  }
}

d <- d %>% group_by(case_id) %>% 
  mutate(community = ifelse(is.na(qns7), id.community(qns6), qns7)) %>% ungroup %>%
  mutate(community = factor(community, levels = 1:4, labels = c("terrebonne", "lafourche", "grand.isle", "plaquemines")),
         fips = qns6) %>%
  mutate(terrebonne = ifelse(community == "terrebonne", 1, 0),
         lafourche = ifelse(community == "lafourche", 1, 0))

### SELECTION INTO STUDY
# We only focus on the study outcome of people who were affected by the spill.

d <- d %>% mutate(affected.by.spill = factor(qn1, levels = 1:2, labels = c("Yes", "No")),
                  experienced.damage = factor(qn8, levels = 1:2, labels = c("Yes", "No")),
                  experienced.damage = ifelse(experienced.damage == "Yes", 1, 0))


### KEY FINAL OUTCOMES
# Let's start by looking at the distributions of the key outcome variables: (1) Q4: Acquisition of resources; (Q5) Speed of Resource Aquisition; (Q7) Satisfaction with Resources

d <- d %>% 
  mutate(resources.acquired = factor(qn4, levels = 1:2, labels = c("Yes", "No")),
         resources.acquired = ifelse(resources.acquired == "Yes", 1, 0),
         resource.acquisition.speed = factor(qn5, levels = 1:5, labels = c("< 1 wk", "1-2 wks", "3-4 wks", "1-2 mos", "> 2 mos")),
         satisfied.with.resources = factor(qn7, levels = 1:2, labels = c("Yes", "No")),
         satisfied.with.resources = ifelse(satisfied.with.resources == "Yes", 1, 0))

#The dispersion in outcome variables is encouraging for statistical analysis. We focus first the question of whether the respondent *received necessary resources*. 

### INTERMEDIATE OUTCOMES
#We want to know whether the people understood the information they received and were able to leverage that information to getting resources.

d <- d %>% mutate(file.claim = factor(qn9, 1:2, labels = c("Yes", "No")), ### ONLY ANSWERED IF PROPERTY DAMAGE
                  file.claim = ifelse(file.claim == "Yes", 1, 0),
                  info.was.helpful = factor(qn12, 1:5, labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,          
                  new.info.understanding = factor(qn11, 1:5, labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric)


### INDIVIDUAL LEARNING CHARACTERISTICS

#1. Prior Knowledge
#  * Knowledge related to oil spills (Q10)
#  * Experience related to natural disasters (Q14)
#2. Individual problem-solving capacity
#  * Education level (Q29)
#  * Adaptive capacity (Q18)
#3. External orientation for solving problems
#  * Attendance at meetings (Q13)
#  * Community monitoring (Q24)
#  * Learning from others (Q20)
#  * Community involvement (Q15)
#4. Information Diversity/Network
#  * Diversity of information sources (Q2/Q3)

d <- d %>% 
  mutate(prior.knowledge.oil.spill = factor(qn10, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         prior.knowledge.disaster.response = factor(qn14, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         highest.education = factor(qn29, levels = 1:8,
                            labels = c("Less than HS", "HS incomplete", "HS graduate", "Some college", "Two year degree", "Four year degree", "Some graduate", "Postgraduate or professional degree")) %>% as.numeric,
         highest.education = case_when(
           highest.education %in% 1:2 ~ 0,
           highest.education %in% 3:6 ~ 1,
           highest.education %in% 6:8 ~ 2
         ),
         adaptive.capacity = factor(qn18, levels = 1:5, labels = c("Not well at all", "Not very well", "Moderately Well", "Very Well", "Extremely Well")) %>% as.numeric,
         cooperative.solutions = factor(qn19, levels = c(1:5),
                                                 labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         use.info.from.others = factor(qn20, levels = c(1:5),
                                                 labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         personal.community.involvement = ifelse(qn1501 == 8, 0, ifelse(!is.na(qn1504),
                                                               4, ifelse(!is.na(qn1503),
                                                                         3, ifelse(!is.na(qn1502), 2, 1)))),
         personal.community.monitoring = factor(qn24, levels = c(1:5),
                                                 labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         training.sessions = factor(qn13, levels = 2:1, labels = c("No", "Yes")),
         training.sessions = ifelse(training.sessions == "Yes", 1, 0))

# Next, we examine sources of information.

d <- d %>%
    mutate(university.source = ifelse(qn201 == 1 | 
                                        qn202 == 1 |
                                        qn203 == 1 |
                                        qn204 == 1 |
                                        qn205 == 1 |
                                        qn206 == 1 |
                                        qn207 == 1 |
                                        qn208 == 1, 1, 0),
           local.pol.source = ifelse(qn201 == 2 | 
                                        qn202 == 2 |
                                        qn203 == 2 |
                                        qn204 == 2 |
                                        qn205 == 2 |
                                        qn206 == 2 |
                                        qn207 == 2 |
                                        qn208 == 2, 1, 0),
           fema.source = ifelse(qn201 == 3 | 
                                        qn202 == 3 |
                                        qn203 == 3 |
                                        qn204 == 3 |
                                        qn205 == 3 |
                                        qn206 == 3 |
                                        qn207 == 3 |
                                        qn208 == 3, 1, 0),
           ngo.source = ifelse(qn201 == 4 | 
                                        qn202 == 4 |
                                        qn203 == 4 |
                                        qn204 == 4 |
                                        qn205 == 4 |
                                        qn206 == 4 |
                                        qn207 == 4 |
                                        qn208 == 4, 1, 0),
           industry.source = ifelse(qn201 == 5 | 
                                        qn202 == 5 |
                                        qn203 == 5 |
                                        qn204 == 5 |
                                        qn205 == 5 |
                                        qn206 == 5 |
                                        qn207 == 5 |
                                        qn208 == 5, 1, 0),
           state.govt.source = ifelse(qn201 == 6 | 
                                        qn202 == 6 |
                                        qn203 == 6 |
                                        qn204 == 6 |
                                        qn205 == 6 |
                                        qn206 == 6 |
                                        qn207 == 6 |
                                        qn208 == 6, 1, 0),
           global.inst.source = ifelse(qn201 == 7 | 
                                        qn202 == 7 |
                                        qn203 == 7 |
                                        qn204 == 7 |
                                        qn205 == 7 |
                                        qn206 == 7 |
                                        qn207 == 7 |
                                        qn208 == 7, 1, 0),
           family.friend.source = ifelse(qn201 == 8 | 
                                        qn202 == 8 |
                                        qn203 == 8 |
                                        qn204 == 8 |
                                        qn205 == 8 |
                                        qn206 == 8 |
                                        qn207 == 8 |
                                        qn208 == 8, 1, 0),
           news.source = ifelse(qn201 == 9 | 
                                        qn202 == 9 |
                                        qn203 == 9 |
                                        qn204 == 9 |
                                        qn205 == 9 |
                                        qn206 == 9 |
                                        qn207 == 9 |
                                        qn208 == 9, 1, 0),
           web.source = ifelse(qn201 == 10 | 
                                        qn202 == 10 |
                                        qn203 == 10 |
                                        qn204 == 10 |
                                        qn205 == 10 |
                                        qn206 == 10 |
                                        qn207 == 10 |
                                        qn208 == 10, 1, 0)) %>%
  mutate_at(vars(university.source:web.source), function(x) ifelse(is.na(x), 0, x)) 
d <- d %>% rowwise() %>%
  mutate(general.source.diversity = sum(university.source, 
                                        local.pol.source, 
                                        fema.source,
                                        ngo.source,
                                        industry.source,
                                        state.govt.source,
                                        global.inst.source,
                                        family.friend.source,
                                        news.source,
                                        web.source, na.rm = T))

# Next, where did you go after the spill.
d <- d %>%
  mutate(university.intepret.spill = factor(qn3a, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         local.pol.intepret.spill = factor(qn3b, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         fema.intepret.spill = factor(qn3c, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         ngo.intepret.spill = factor(qn3d, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         industry.intepret.spill = factor(qn3e, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         state.gov.intepret.spill = factor(qn3f, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         global.inst.intepret.spill = factor(qn3g, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         family.friends.intepret.spill = factor(qn3h, levels = 1:5, 
                                  c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric) %>%
  rowwise() %>%
  mutate(spill.source.diversity = sum(university.intepret.spill >= 3,
           local.pol.intepret.spill >= 3,
           fema.intepret.spill >= 3,
           ngo.intepret.spill >= 3,
           industry.intepret.spill >= 3, 
           state.gov.intepret.spill >= 3,
           global.inst.intepret.spill >= 3,
           family.friends.intepret.spill >= 3, na.rm = T)) %>% 
  ungroup

#Community facilitation of learning
#1. External Orientation (Formal and Informal)
#  * External information sources (Q21)
#  * Usefulness of new knowledge (Q22)
#  * New arrangements to work together (Q16)
#2. Community Cohesion (Formal and Informal)
#  * Frequency of Meetings (Q23)
#  * Feelings of Kinship (Q25, Q26)
#  * Favors (Q27)
  
d <- d %>% 
  mutate(community.new.knowledge.useful = factor(qn21, levels = c(1:5),
                                                 labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         community.external.info.sources = factor(qn22, levels = c(1:5),
                                                 labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         community.meeting.frequency = factor(qn23, levels = c(1:5),
                                                 labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         community.family = factor(qn25, levels = c(1:5),
                                                 labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         community.criticism.insult = factor(qn26, levels = c(1:5),
                                                 labels = c("Not at all", "Not too much", "Somewhat", "Very much", "A great deal")) %>% as.numeric,
         community.favors = factor(qn27, levels = c(6:1),
                                                 labels = c("Basically every day", "A few times per week", "A few times per month", "Once per month", "< 1 per month", "Not at all")) %>% as.numeric,
         new.community.arrangements = factor(qn16, levels = 2:1, labels = c("No", "Yes")),
         new.community.arrangements = ifelse(new.community.arrangements == "Yes", 1, 0)) %>%
  rowwise() %>%
  mutate(community.cohesion = sum(community.family, community.criticism.insult, community.favors, community.meeting.frequency, na.rm = T),
         community.external.orientation = sum(community.new.knowledge.useful, 
                                              community.external.info.sources, 
                                              new.community.arrangements, 
                                              na.rm = T)) %>%
  ungroup()

### Other controls
d <- d %>% mutate(tenure.in.community = factor(qn28, levels = 1:6,
                                      labels = c("< 2 years", "2-4 years", "5-9 years", "10-14 years", "14-19 years", "> 20 years")) %>% as.numeric,
         occupation = factor(qn28a, levels = 1:7,
                             labels = c("Fisherman", "Tour operator", "Oil or gas worker", "Teacher", "Government official", "Researcher", "Other")),
         race = factor(qn31, levels = 1:7, labels = c("White", "Black", "Asian", "Native American", "Pacific Islander", "Mixed", "Other")),
         hispanic = factor(qn30, levels = c(1:2, 8), labels = c("Yes", "No", "Don't Know")),
         people.in.hh = ifelse(qn32 < 9, qn32, NA),
         adults.in.hh = ifelse(qn32a < 9, qn32, NA))

# create dummy variables for controls.

related.occupations = c("Fisherman", "Tour operator", "Oil or gas worker",
                        "Government official", "Researcher")
d <- d %>% mutate(related.occupation = ifelse(occupation %in% related.occupations, 1, 0),
                  hispanic = ifelse(hispanic == "Yes", 1, 0),
                  nonhispanic.white = ifelse(hispanic == 0 & race == "White", 1, 0),
                  black = ifelse(hispanic == 0 & race == "Black", 1, 0),
                  male = ifelse(qns1 == 1, 1, 0))

# generate models to examine the three outcomes:
# * Whether resources were acquired.
# * Whether the individual received helpful information
# * Whether the information understood information received
 
# These are our preferred models.
d <- d %>% filter(affected.by.spill == "Yes" & qn4 %in% 1:2)

### SEPARATE MODELS
m1<-zelig(resources.acquired ~  info.was.helpful + lafourche + terrebonne + nonhispanic.white, model = "probit.survey", data = d, weight = weight2)
summary(m1)   
          
m2<-zelig(info.was.helpful ~ new.info.understanding + community.external.orientation + community.cohesion + prior.knowledge.disaster.response + spill.source.diversity + spill.source.diversity:new.info.understanding +  experienced.damage + terrebonne + lafourche + tenure.in.community + related.occupation + nonhispanic.white + hispanic + black + male + agegrp, model = "poisson.survey", data = d, weight = weight2)
summary(m2)   

m3<-zelig(new.info.understanding ~  prior.knowledge.oil.spill + training.sessions + use.info.from.others + adaptive.capacity + cooperative.solutions +  highest.education +  terrebonne + lafourche + tenure.in.community + related.occupation + nonhispanic.white + hispanic + black + male + agegrp, model = "poisson.survey", data = d, weight = weight2)
summary(m3)   

#These are the models that test the overall structure using structural equation modeling. The models here are two-stage least squares models. 

#The first examines whether the individual and community characteristics described above generate sufficient variance in whether information was helpful in order to influence whether necessary resoures were acquired. 

#The idea is that resource acquisition is a function of whether information is helpful and controls; and whether information is helpful is a function of information understanding, community external orientation, community cohesion, prior knowledge related to disasters, diversity of sources of information about the spill, and controls. The results are highly significant (indicating this a good model).

#The second examines whether the individual and community characteristics described above generate sufficient variance in whether information was understood in order to influence whether information was helpful.

#The theory is that helpfulness of information is a function of new information understanding, community external orientation, community cohesion, prior knowledge related to disasters, diversity of sources of information about the spill, and controls. Whether new information is understood is a function of prior knowledge about oil spills, attendance of training sessions, ability to for the individual to use info from others, to use new information to cope, and ability cooperate in order to solve new problems. Here, the inputs into understanding do not drive enough variance in the level of understanding to significantly impact whether the information received was helpful.

### 2SLS
eq1 <- tsls(resources.acquired ~  info.was.helpful + experienced.damage + info.was.helpful + experienced.damage + lafourche + terrebonne + nonhispanic.white + agegrp,
            ~new.info.understanding + community.external.orientation + community.cohesion + prior.knowledge.disaster.response + spill.source.diversity + male + experienced.damage + lafourche + terrebonne + nonhispanic.white + agegrp, data = d)
summary(eq1)

eq2 <- tsls(info.was.helpful ~ new.info.understanding + experienced.damage + community.external.orientation + community.cohesion + prior.knowledge.disaster.response + spill.source.diversity + nonhispanic.white + agegrp,
            ~ prior.knowledge.oil.spill + training.sessions + use.info.from.others + adaptive.capacity + cooperative.solutions + experienced.damage + community.external.orientation + community.cohesion + prior.knowledge.disaster.response + spill.source.diversity + highest.education + male + nonhispanic.white + agegrp, data = d)
summary(eq2)

eq3 <- lm(new.info.understanding ~ prior.knowledge.oil.spill + highest.education + adaptive.capacity + training.sessions + personal.community.monitoring + use.info.from.others + personal.community.involvement + experienced.damage + lafourche + terrebonne + nonhispanic.white + agegrp, data = d)
summary(eq3)

